package fz.vrd.ui.view.rcv;


import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private SparseArray<View> mViews;

    private View view;

    public RecyclerViewHolder(@NonNull View itemView) {
        super(itemView);
    }

    public RecyclerViewHolder(final View itemView, final BaseRecyclerAdapter adapter, final RecylerViewItemClickListern listern) {
        super(itemView);
        view = itemView;
        mViews = new SparseArray<View>();
        itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listern != null && adapter.getListData().size() > getAdapterPosition()) {
                    if (adapter.getItemViewType(0) == RecItemType.ITEM_TYPE_HEADER) {
                        listern.clickListern(view, adapter.getListData().get(getAdapterPosition() - 1), getAdapterPosition() - 1);
                    } else {
                        listern.clickListern(view, adapter.getListData().get(getAdapterPosition()), getAdapterPosition());
                    }
                }
            }
        });
    }

    public <T extends View> T getView(int viewId) {
        View v = mViews.get(viewId);
        if (v == null) {
            v = view.findViewById(viewId);
            mViews.put(viewId, v);
        }
        return (T) v;
    }

    public void setViewValue(int viewId, String value) {
        View view = getView(viewId);
        if (view != null) {
            if (view instanceof TextView) {
                ((TextView) view).setText(value);
            } else if (view instanceof EditText) {
                ((EditText) view).setText(value);
            } else if (view instanceof Button) {
                ((Button) view).setText(value);
            }
        }
    }
}
